###############################################################
###############################################################
# Monday 19 December 2011


import csv


###############################################################
# This function reads the csv file "filename" and extracts the
# items from each row as a list and so the csv file is represented
# by a list of lists.

def domydb_new(filename):
	returnList = []
	r = csv.DictReader(filename)
	for line in r:
		returnList.append(line)
	return returnList

def domydb(filename):
    mydb = []
    myid = 0
    with open(filename, 'rb') as f:
        reader = csv.reader(f)
        for row in reader:
            if row[0] != '':
                myrow = []
                #print row[0]
                myrow.append(myid)
                myid = myid+1
                for c in row:
                    myrow.append(c)
                mydb.append(myrow)
        return mydb



###############################################################
# This function builds the evidence table
# The list of lists generated by the above function
# is transformed into the evidence table
# by transforming each row into a dictionary.
# So the evidence table is a list of dictionaries.
# The keys for the dictionary are the column headings from
# the spreadsheet.

def dodictionary(mytempdb):
    mydictionarylist = []
    headings = mytempdb[0]
    mytempdb.remove(mytempdb[0])
    limit = len(headings)
    for x in mytempdb:
        mydictionary = {}
        mydictionary["myid"] = x[0]
        counter = 1
        while counter < limit:
            mydictionary[headings[counter]] = x[counter]
            counter = counter+1
        #print mydictionary
        mydictionarylist.append(mydictionary)
    return mydictionarylist
        
    


###############################################################
# This function combines the above two functions to
# produce the evidence table from the csv fule.

def getmydb(myfilename):
    tempdb = domydb(myfilename)
    tempdb2 = dodictionary(tempdb)
    return tempdb2

#########################################

